package com.dfbz.controller.dept;

import com.dfbz.controller.BaseServlet;
import com.dfbz.service.impl.DeptDaoServletImp;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@WebServlet("/dept/*")
public class FindAllDept extends BaseServlet {

    public void findAllDept(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        HashMap<String, ArrayList<Map<String, Object>>> listHashMap = new HashMap<>();
        HashMap<Object, Integer> integerHashMap = new HashMap<>();
        DeptDaoServletImp deptDaoServletImp = new DeptDaoServletImp();
        List<Map<String, Object>> allDept = deptDaoServletImp.findAllDept();
        for (Map<String, Object> map : allDept) {
            Object uId = "uId";
            Object deptName = "deptName";
            Object o1 = map.get(uId);
            String string = map.get(deptName).toString();
            if (null == listHashMap.get(string)) {
                ArrayList<Map<String, Object>> maps = new ArrayList<>();
                if (null != o1) {
                    maps.add(map);
                }
                listHashMap.put(string, maps);
            } else {
                listHashMap.get(string).add(map);
            }
        }
        Set<Map.Entry<String, ArrayList<Map<String, Object>>>> entries = listHashMap.entrySet();
        for (Map.Entry<String, ArrayList<Map<String, Object>>> haspmap : entries) {
            int size = haspmap.getValue().size();
            integerHashMap.put(haspmap, size);
        }
        req.setAttribute("AllDept", integerHashMap);
        req.getRequestDispatcher("/html/department.jsp").forward(req, resp);
    }


}
